package method.searchSQL;

//StockControlSys
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class typeControl {
	ArrayList<Type> type = new ArrayList<Type>();
	Type t ;
	
	public typeControl(){
	}
	
	
	/////***** Status Management *****/////
	public void addType(String databaseURL){
		try{
			HttpClient client = new DefaultHttpClient();
			HttpGet request = new HttpGet(databaseURL);
			HttpResponse response = client.execute(request);
			String line = "";
			String reply = "";
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					response.getEntity().getContent()));
			while ((line = reader.readLine()) != null) {
				reply += line;
			}
			reader.close();
			try {

				JSONArray json = new JSONArray(reply);
				JSONArray jsonArrayB = new JSONArray(json.getString(0));
				
				for(int i = 0; i< jsonArrayB.length(); i++){
					String getField = jsonArrayB.getString(i);
					JSONObject jsonField = new JSONObject(getField);
					String typeIDValue  = jsonField.getString("typeID");
					String typeNameValue = jsonField.getString("typeName");
					String descriptionValue = jsonField.getString("description");
					String commonNameValue = jsonField.getString("commonName");

					t = new Type(typeIDValue, typeNameValue, descriptionValue, commonNameValue);
					type.add(t);					
				}


			} catch (JSONException e) {
				t = new Type("x", "y", "z", "w");
				type.add(t);
			}
		}
		catch (Exception e) {
			t = new Type("x", "y", "z", "w");
			type.add(t);
		}
		
	}

	public String getType(int position){
		Type currentType = type.get(position);
		String s = new String(""+currentType.gettypeName());
		return s;
	}
	
	public int getSize(){
		return type.size();
	}
	
	public String searchTypeIDByTypeName(String typeValue){
		int index =-1;
		String y;
		Type x;
		String id;

		for(int i = 0; i < type.size() ; i++) {
			x = type.get(i);
			y = ""+x.gettypeName();
			if(y.equals(typeValue))
				index = i;
		}
		
		Type currentType = type.get(index);
		String s = new String(""+currentType.gettypeID());
		
		return s;
	}
	
	public String toString(){
		String string = new String(""+type);
		return string;
	}
	
	public List getTypeList(){
		return type;
	}
	
///////////////////////no use////////////////////////////
	/*
	public int getIndex(Status s){
			return type.indexOf(s);
		}
	public Type getSearch(String g){
			return type.get(type.indexOf(g));
		}



	public String searchStatusObjectByStatus(String statusValue){
		int index =-1;
		String y;
		Status x;
		String id;

		for(int i = 0; i < status.size() ; i++) {
			x = status.get(i);
			y = ""+x.getstatus();
			if(y.equals(statusValue))
				index = i;
		}
		
		id = new String(""+status.get(index));
		return id;
	}
	 */
}
